package cn.skyisazure.metacompare.other;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 可执行的sql和参数
 *
 * @author wangjj
 * @date 2023/11/11 14:27
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SqlModel {

    private String sql;
    private List<Object> params;

    /*没有条件的sql*/
    public SqlModel(String sql) {
        this.sql = sql;
        this.params = Collections.emptyList();
    }

    /**
     * 根据模板和值列表生成SQL语句
     *
     * @return 生成的SQL语句
     * @throws IllegalArgumentException 如果sqlTemplate或values为null抛出此异常
     */
    public String generateSql() {
        if (sql == null || params == null) {
            throw new IllegalArgumentException("SQL template and values cannot be null");
        }

        StringBuilder finalSql = new StringBuilder();

        char[] charArray = sql.toCharArray();
        // 占位符号的下标值
        List<Integer> indexs = new ArrayList<>();
        {// 获取?字段的位置，排除字符串类型的占位符
            boolean ship = false;
            for (int i = 0; i < charArray.length; i++) {
                char c = charArray[i];
                if (ship) {
                    if (c == '\'') {
                        ship = false;
                    }
                } else {
                    if (c == '\'') {
                        ship = true;
                    } else if (c == '?') {
                        indexs.add(i);
                    }
                }
            }
        }

        // 把原始sql分成几段（按indexs分），拼接段即可
        List<String> splits = new ArrayList<>();
        if (indexs.isEmpty()) {
            // 没有参数，直接一句话返回
            return sql;
        }
        // 假如，20，30-> 0~19，30~最后
        splits.add(sql.substring(0, indexs.get(0)));
        for (int i = 1; i < indexs.size(); i++) {
            splits.add(sql.substring(indexs.get(i - 1) + 1, indexs.get(i)));
        }
        splits.add(sql.substring(indexs.get(indexs.size() - 1) + 1));
        /*if (params.size() != splits.size()-1) {
            throw new IllegalArgumentException("参数数量与占位符数量不匹配");
        }*/
        for (int i = 0; i < splits.size() - 1; i++) {
            finalSql.append(splits.get(i));
            Object value = params.get(i);
            // 字符串加引号、其他类型直接添加
            if (value instanceof String || value instanceof Character) {
                finalSql.append("'").append(value).append("'");
            } else {
                finalSql.append(value);
            }
        }
        finalSql.append(splits.get(splits.size() - 1));

        return finalSql.toString();
    }

    @Override
    public String toString() {
        /*return "SqlModel{" +
                "sql='" + sql + '\'' +
                ", params=" +  +
                '}';*/
        return "语句：" + "\n" +
                sql + "\n" +
                "参数：" + "\n" +
                Arrays.toString(params.toArray());
    }
}
